package com.future.mayi.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.RegisterActivity;
import com.future.mayi.widget.PhoneCodeView;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

public class RegisterCodeFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.phone_code_view)
    PhoneCodeView phoneCodeView;
    @BindView(R.id.btn_enter)
    Button btnEnter;
    private int type;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_register_code;
    }

    @Override
    protected void initView() {
        setType(type);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }


    public void setEnabled(boolean clickable) {
        if (tvCode != null)
            tvCode.setEnabled(clickable);
    }

    public void setTime(int time) {
        if (tvCode == null) return;
        if (time == 0)
            tvCode.setText("重新发送验证码");
        else
            tvCode.setText(time + "s后重试");
    }

    public void setPhone(String phone) {
        if (tvHint == null) return;
        tvHint.setText("验证码已发送至" + phone);
        if (phoneCodeView != null)
            phoneCodeView.showSoftInput();


    }


    public void setType(int type) {
        this.type = type;
        if (btnEnter != null)
            btnEnter.setText(type == 3 ? "绑定手机号" : (type == 2 ? "立即注册" : "立即登录"));
    }


    @OnClick({R.id.tv_code, R.id.btn_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_code:
                ((RegisterActivity) getActivity()).getCodeAgain();
                break;
            case R.id.btn_enter: {
                btnEnter.setEnabled(false);
                String code = phoneCodeView.getPhoneCode();
                if (TextUtils.isEmpty(code) || code.length() < 6) {
                    ToastUtil.getInstance().show(getActivity(), "请输入正确的验证码");
                    btnEnter.setEnabled(true);
                    return;
                }
                ((RegisterActivity) getActivity()).enter(code);
                btnEnter.setEnabled(true);
            }
            break;
        }
    }

}
